<template>
  <div class="app-container">
    <div v-show="show">
      <!-- 查询表单部分保持不变 -->
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
        size="medium"
        class="ry_form"
      >
        <el-form-item label="编码" prop="outProductId">
          <el-input
            v-model="queryParams.outProductId"
            placeholder="请输入商品编码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="flex_one tr">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <!-- 表格部分保持不变 -->
      <el-table
        v-loading="loading"
        :data="pmsProductList"
        @selection-change="handleSelectionChange"
        border
      >
        <!-- 表格列定义保持不变 -->
        <el-table-column type="selection" width="30" align="center" />
        <el-table-column label="主图" prop="pic" min-width="30">
          <template slot-scope="{ row }">
            <el-image
              v-if="row.pic"
              :src="row.pic"
              :preview-src-list="[row.pic]"
              class="small-img"
            />
          </template>
        </el-table-column>
        <el-table-column label="名称/编码" min-width="150" prop="outProductId">
          <template slot-scope="{ row }">
            <div>名称：{{ row.name }}</div>
            <div v-if="row.outProductId">编码：{{ row.outProductId }}</div>
            <div v-if="row.weight">斤数：{{ row.weight }} {{ row.unit }}</div>
          </template>
        </el-table-column>
        <el-table-column label="规格/分类" prop="brandName" min-width="60">
          <template slot-scope="{ row }">
          <div v-if="row.specifications">
              规格：{{ row.specifications }}
            </div>
            <div v-if="row.productCategoryName">
              分类：{{ row.productCategoryName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="预期价格（斤/元）" prop="expectedPrice">
          <template slot-scope="{ row }">
            <div v-if="row.expectedPrice">价格：{{ row.expectedPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column label="售卖方式" prop="salesMethodName" min-width="50">
          <template slot-scope="{ row }">
            <div v-if="row.salesMethodName">{{ row.salesMethodName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="产地" prop="province" min-width="35">
          <template slot-scope="{ row }">
            <div v-if="row.province">{{ row.province }}</div>
          </template>
        </el-table-column>
        <el-table-column label="会员信息" prop="phone">
          <template slot-scope="{ row }">
            <div v-if="row.nickname">昵称：{{ row.nickname }}</div>
            <div v-if="row.phone">联系电话: {{ row.phone }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          class-name="small-padding fixed-width"
          width="55"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.storeStatus === 1" class="status-tag success">
              已入库
            </div>
            <el-button
              v-else
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleStore(scope.row)"
              >入库
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 修复分页组件 -->
      <div v-show="total>0">
        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  listPmsMemberProduct,
  delPmsMemberProduct,
  productStore,
} from "@/api/pms/memberProduct";
import { mapGetters } from "vuex";

export default {
  name: "PmsProduct",
  components: {},
  dicts: ["pms_publish_status", "pms_quality_inspection_status"],
  data() {
    return {
      show: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      pmsProductList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outProductId: null,
        phone: null,
      },
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(["userId"]),
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      const { pageNum, pageSize } = this.queryParams;
      const query = {
        ...this.queryParams,
        pageNum: undefined,
        pageSize: undefined,
      };
      const pageReq = { page: pageNum - 1, size: pageSize };
      listPmsMemberProduct(query, pageReq).then((response) => {
        const { content, totalElements } = response;
        this.pmsProductList = content;
        this.total = totalElements;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPmsMemberProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 入库按钮操作 */
    handleStore(row) {
      const ids = [row.id] || this.ids;
      this.$modal
        .confirm('是否确认该产品编号为"' + row.outProductId + '"的入库？')
        .then(function () {
          return productStore(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("入库成功");
        })
        .catch(() => {});
    },
  },
};
</script>
